package com.kt.game1

import java.awt.Color
import java.awt.Dimension
import java.awt.Font
import java.awt.event.KeyAdapter
import java.awt.event.KeyEvent
import javax.swing.JFrame
import javax.swing.JLabel

class Game : JFrame() {
    private val label = JLabel()
    private var snake = mutableListOf(Pair(5, 5))
    private var food = Pair(10, 10)
    private var direction = "R"
    private var gameOver = false
    private val gridSize = 20
    private val gameWidth  = 400
    private val gameHeight  = 400

    init {
        defaultCloseOperation = JFrame.EXIT_ON_CLOSE
        setSize(gameWidth, gameHeight)
        title = "Snake Game"
        add(label)
        label.preferredSize = Dimension(width, height)
        label.font = Font("Arial", Font.BOLD, 20)
        label.foreground = Color.BLACK
        addKeyListener(object : KeyAdapter() {
            override fun keyPressed(e: KeyEvent?) {
                if (e != null && !gameOver) {
                    when (e.keyCode) {
                        KeyEvent.VK_UP -> if (direction != "D") direction = "U"
                        KeyEvent.VK_DOWN -> if (direction != "U") direction = "D"
                        KeyEvent.VK_LEFT -> if (direction != "R") direction = "L"
                        KeyEvent.VK_RIGHT -> if (direction != "L") direction = "R"
                    }
                }
            }
        })
        isVisible = true
        gameLoop()
    }

    private fun gameLoop() {
        while (!gameOver) {
            moveSnake()
            checkCollision()
            repaint()
            try {
                Thread.sleep(200)
            } catch (e: InterruptedException) {
                e.printStackTrace()
            }
        }
    }

    private fun moveSnake() {
        val head = snake[0]
        var newHead = head
        when (direction) {
            "U" -> newHead = Pair(head.first, head.second - 1)
            "D" -> newHead = Pair(head.first, head.second + 1)
            "L" -> newHead = Pair(head.first - 1, head.second)
            "R" -> newHead = Pair(head.first + 1, head.second)
        }

        snake.add(0, newHead)

        if (newHead == food) {
            generateFood()
        } else {
            snake.removeAt(snake.size-1)
        }
    }

    private fun checkCollision() {
        val head = snake[0]
        if (head.first < 0 || head.first >= width / gridSize || head.second < 0 || head.second >= height / gridSize) {
            gameOver = true
        }
        if (snake.subList(1, snake.size).contains(head)) {
            gameOver = true
        }
    }

    private fun generateFood() {
        var newFood: Pair<Int, Int>
        do {
            newFood = Pair((0 until width / gridSize).random(), (0 until height / gridSize).random())
        } while (snake.contains(newFood))
        food = newFood
    }

    override fun paint(g: java.awt.Graphics?) {
        super.paint(g)
        g?.color = Color.WHITE
        g?.fillRect(0, 0, width, height)

        g?.color = Color.GREEN
        for ((x, y) in snake) {
            g?.fillRect(x * gridSize, y * gridSize, gridSize, gridSize)
        }

        g?.color = Color.RED
        val (fx, fy) = food
        g?.fillRect(fx * gridSize, fy * gridSize, gridSize, gridSize)

        if (gameOver) {
            g?.color = Color.BLACK
            g?.font = Font("Arial", Font.BOLD, 30)
            g?.drawString("Game Over!", width / 4, height / 2)
        }
    }
}

fun main() {
    Game()
}